package adapter;

import java.util.ArrayList;

import com.qzhy.female.R;

import Constant.Constance;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import model.Category_Result;
import utils.UILUtils;

public class MyPostAdapter extends BaseAdapter{

	private Context context;
	private ArrayList<Category_Result> mArticles;
	private ViewHold viewHold;

	public MyPostAdapter(Context context) {
		this.context = context;
		
	}
	public MyPostAdapter(Context context, ArrayList<Category_Result> mArticles) {
		this.context = context;
		// TODO Auto-generated constructor stub
		this.mArticles = mArticles;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArticles.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null){
			convertView=View.inflate(context, R.layout.item_my_post_list, null);
			viewHold = new ViewHold();
			viewHold.Img1=(ImageView) convertView.findViewById(R.id.img_photo);
			viewHold.Img2=(ImageView) convertView.findViewById(R.id.img_photo2);
			viewHold.Img3=(ImageView) convertView.findViewById(R.id.img_photo3);
			viewHold.TvContent=(TextView) convertView.findViewById(R.id.tv_content);
			viewHold.TvPhotoNum=(TextView) convertView.findViewById(R.id.tv_photo_num);
			viewHold.ImgCircleIcon=(ImageView) convertView.findViewById(R.id.img_circle_icon);
			viewHold.TvCircleName=(TextView) convertView.findViewById(R.id.tv_circle_name);
			viewHold.TvPostNum=(TextView) convertView.findViewById(R.id.tv_post_num);
			convertView.setTag(viewHold);
		}else {
			viewHold = (ViewHold) convertView.getTag();
		}
		int screenWidth = utils.ScreenUtils.getScreenWidth(context);
		viewHold.TvContent.setText(mArticles.get(position).getContent());
		viewHold.TvPostNum.setText("回复 "+mArticles.get(position).getComments().size());
		viewHold.TvCircleName.setText(mArticles.get(position).getCategory().getName());
		UILUtils.displayImageNoAnim(mArticles.get(position).getCategory().getThumbnail(), viewHold.ImgCircleIcon);
		int Imgsize = mArticles.get(position).getImages().size();
		if(Imgsize==0){
			viewHold.Img1.setVisibility(View.GONE);
			viewHold.Img2.setVisibility(View.GONE);
			viewHold.TvPhotoNum.setVisibility(View.GONE);
			viewHold.Img3.setVisibility(View.GONE);
		}else if(Imgsize==1){
			viewHold.Img1.setVisibility(View.VISIBLE);
			viewHold.Img1.setLayoutParams(new LinearLayout.LayoutParams(screenWidth/3, LayoutParams.MATCH_PARENT));
			
			if(mArticles.get(position).getImages().get(0).getUrl().startsWith("http")){
				UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(0).getUrl(), viewHold.Img1);
			}else {
				UILUtils.displayImageNoAnim(Constance.URL.IMG_BASE+"/"+mArticles.get(position).getImages().get(0).getUrl(), viewHold.Img1);
			}
			viewHold.Img2.setVisibility(View.INVISIBLE);
			viewHold.TvPhotoNum.setVisibility(View.INVISIBLE);
			viewHold.Img3.setVisibility(View.INVISIBLE);
		}else if (Imgsize==2) {
			viewHold.Img1.setVisibility(View.VISIBLE);
			viewHold.Img1.requestLayout();
			viewHold.Img2.requestLayout();
			viewHold.Img1.setLayoutParams(new LinearLayout.LayoutParams(screenWidth/3, LayoutParams.MATCH_PARENT));
			viewHold.Img2.setLayoutParams(new LinearLayout.LayoutParams(screenWidth/3, LayoutParams.MATCH_PARENT));
			if(mArticles.get(position).getImages().get(0).getUrl().startsWith("http")){
				UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(0).getUrl(), viewHold.Img1);
				UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(1).getUrl(), viewHold.Img2);
			}else {
				UILUtils.displayImageNoAnim(Constance.URL.IMG_BASE+"/"+mArticles.get(position).getImages().get(0).getUrl(), viewHold.Img1);
				UILUtils.displayImageNoAnim(Constance.URL.IMG_BASE+"/"+mArticles.get(position).getImages().get(1).getUrl(), viewHold.Img2);
			}
			viewHold.Img2.setVisibility(View.VISIBLE);
			viewHold.TvPhotoNum.setVisibility(View.GONE);
			viewHold.Img3.setVisibility(View.GONE);
		}else if (Imgsize==3) {
			if(mArticles.get(position).getImages().get(0).getUrl().startsWith("http")){
				UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(0).getUrl(), viewHold.Img1);
				UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(1).getUrl(), viewHold.Img2);
				UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(2).getUrl(), viewHold.Img3);
			}else {
				UILUtils.displayImageNoAnim(Constance.URL.IMG_BASE+"/"+mArticles.get(position).getImages().get(0).getUrl(), viewHold.Img1);
				UILUtils.displayImageNoAnim(Constance.URL.IMG_BASE+"/"+mArticles.get(position).getImages().get(1).getUrl(), viewHold.Img2);
				UILUtils.displayImageNoAnim(Constance.URL.IMG_BASE+"/"+mArticles.get(position).getImages().get(2).getUrl(), viewHold.Img3);
			}
			viewHold.Img2.setVisibility(View.VISIBLE);
			viewHold.Img3.setVisibility(View.VISIBLE);
			viewHold.TvPhotoNum.setVisibility(View.VISIBLE);
			viewHold.TvPhotoNum.setText("共"+mArticles.get(position).getImages().size()+"张");
		}else {
			if(mArticles.get(position).getImages().get(0).getUrl().startsWith("http")){
				UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(0).getUrl(), viewHold.Img1);
				UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(1).getUrl(), viewHold.Img2);
				UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(2).getUrl(), viewHold.Img3);
			}else {
				UILUtils.displayImageNoAnim(Constance.URL.IMG_BASE+"/"+mArticles.get(position).getImages().get(0).getUrl(), viewHold.Img1);
				UILUtils.displayImageNoAnim(Constance.URL.IMG_BASE+"/"+mArticles.get(position).getImages().get(1).getUrl(), viewHold.Img2);
				UILUtils.displayImageNoAnim(Constance.URL.IMG_BASE+"/"+mArticles.get(position).getImages().get(2).getUrl(), viewHold.Img3);
			}
			
			UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(0).getUrl(), viewHold.Img1);
			UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(1).getUrl(), viewHold.Img2);
			UILUtils.displayImageNoAnim(mArticles.get(position).getImages().get(2).getUrl(), viewHold.Img3);
			viewHold.Img1.setVisibility(View.VISIBLE);
			viewHold.Img2.setVisibility(View.VISIBLE);
			viewHold.Img3.setVisibility(View.VISIBLE);
			viewHold.TvPhotoNum.setVisibility(View.VISIBLE);
			viewHold.TvPhotoNum.setText("共"+mArticles.get(position).getImages().size()+"张");
		}
		return convertView;
	}
	class ViewHold{
		TextView TvContent;
		TextView TvCircleName;
		ImageView ImgCircleIcon;
		ImageView Img1;
		ImageView Img2;
		ImageView Img3;
		TextView TvPhotoNum;
		TextView TvPostNum;
	}

}
